package com.ddwl.goods.controller;

import cn.hutool.core.util.RandomUtil;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.RedisKey;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.service.RedisService;
import com.ddwl.goods.service.ItemAgentService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ItemAgentBo;
import com.ddwl.schema.bo.goods.ItemSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.ItemAgentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/6/14 9:28
 */
@Api(tags = "商品代理服务")
@RequestMapping("item/agent")
@RestController
@RequiredArgsConstructor
@Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.SHOP_CONSOLE})
public class ItemAgentController extends BaseController {

    private final ItemAgentService itemAgentService;
    private final RedisService redisService;

    @PostMapping("page")
    @ApiOperation("分页查询代理商品")
    public RestVo<PageVo<ItemAgentVo>> page(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        return RestVo.ok(itemAgentService.page(pageBo));
    }

    @PostMapping("/mch/page")
    @ApiOperation("分页查询商户代理商品")
    public RestVo<PageVo<ItemAgentVo>> mchPage(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        ItemSearchBo param = Optional.ofNullable(pageBo.getParam()).orElse(new ItemSearchBo());
        param.setMchId(getMchId());
        param.setShopId(getShopId());
        pageBo.setParam(param);
        return RestVo.ok(itemAgentService.page(pageBo));
    }

    @PostMapping("proxy")
    @ApiOperation("代理商品")
    public RestVo<Boolean> proxy(@Valid @RequestBody List<ItemAgentBo> bos) {
        itemAgentService.proxy(bos);
        return RestVo.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改代理商品")
    public RestVo<Boolean> update(@RequestBody ItemAgentBo bo) {
        itemAgentService.update(bo);
        return RestVo.ok();
    }

    @DeleteMapping("del")
    @ApiOperation("删除代理商品")
    public RestVo<Boolean> delete(@RequestParam String id) {
        itemAgentService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("scene")
    @ApiOperation("小程序码scene对应参数")
    @Auth(required = false)
    public RestVo<Map<String, String>> scene(@RequestParam String scene) {
        return RestVo.ok(redisService.getHashOps().entries(RedisKey.SHARE_CODE_INFO.getKey(scene)));
    }

    @GetMapping("info")
    @ApiOperation("店铺代理商品详情")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo<ItemAgentVo> info(@RequestParam String itemId, @RequestParam String shopId) {
        return RestVo.ok(itemAgentService.info(itemId, shopId));
    }

    @GetMapping("/shopIdList")
    @ApiOperation("代理商品的店铺id列表")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<String>> agentShopIds(@RequestParam String itemId) {
        return RestVo.ok(itemAgentService.agentShopIds(itemId));
    }

    @PostMapping("/wxaCode/down")
    @ApiOperation("下载代理商品小程序码")
    public void downWxaCode(HttpServletResponse response, @RequestBody List<String> agentIds) {
        itemAgentService.downWxaCode(response, agentIds, getShopId());
    }


    @PostMapping("/shops")
    @ApiOperation("代理商品的店铺列表")
    public RestVo<PageVo<ItemAgentVo>> shops(@RequestBody PageBo<ItemAgentBo> pageBo) {
        return RestVo.ok(itemAgentService.shops(pageBo));
    }


}
